/**
 * Copyright (c) 2008, Mu Dynamics.
 *  All rights reserved.
 *  
 *   Redistribution and use in source and binary forms, with or without modification, 
 *   are permitted provided that the following conditions are met:
 *   
 *  - Redistributions of source code must retain the above copyright notice, 
 *     this list of conditions and the following disclaimer.
 *  - Redistributions in binary form must reproduce the above copyright notice, 
 *     this list of conditions and the following disclaimer in the documentation and/or 
 *     other materials provided with the distribution.
 *  - Neither the name of the "Mu Dynamics" nor the names of its contributors may be used 
 *     to endorse  or promote products derived from this software without specific prior 
 *     written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 
 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package com.mu.jacob.core.model.decorator;

import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.AnnotationValue;
import javax.lang.model.element.Element;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.Name;
import javax.lang.model.element.PackageElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.util.Elements;

import com.mu.jacob.core.generator.Decorate;
import com.sun.tools.javac.model.JavacElements;
import com.sun.tools.javac.tree.JCTree;

/**
 * Elements decorator
 * @author Adam Smyczek
 *
 */
public class DefaultElements extends DefaultDecorator<Elements> implements Elements {

	/** Decorated elements */
	private Elements elements;
	
	/**
	 * @see com.mu.jacob.core.model.decorator.DefaultDecorator#getDecorated()
	 */
	public Elements getDecorated() {
		return elements;
	}

	/**
	 * @see com.mu.jacob.core.model.decorator.DefaultDecorator#getApiClass()
	 */
	public final Class<Elements> getApiClass() {
		return Elements.class;
	}

	/**
	 * @see com.mu.jacob.core.model.decorator.DefaultDecorator#initialize(java.lang.Object)
	 */
	public void initialize(Elements model) {
		this.elements = model;
	}

	/**
	 * @see javax.lang.model.util.Elements#getName(java.lang.CharSequence)
	 */
	public Name getName(CharSequence cs) {
		return elements.getName(cs);
	}

	/**
	 * @see javax.lang.model.util.Elements#getPackageOf(javax.lang.model.element.Element)
	 */
	@Decorate
	public PackageElement getPackageOf(Element type) {
		return elements.getPackageOf(undeco(type));
	}

	/**
	 * @see javax.lang.model.util.Elements#getAllAnnotationMirrors(javax.lang.model.element.Element)
	 */
	@Decorate
	public List<? extends AnnotationMirror> getAllAnnotationMirrors(Element e) {
		return elements.getAllAnnotationMirrors(undeco(e));
	}

	/**
	 * @see javax.lang.model.util.Elements#getAllMembers(javax.lang.model.element.TypeElement)
	 */
	@Decorate
	public List<? extends Element> getAllMembers(TypeElement type) {
		return elements.getAllMembers(undeco(type));
	}

	/**
	 * @see javax.lang.model.util.Elements#getBinaryName(javax.lang.model.element.TypeElement)
	 */
	public Name getBinaryName(TypeElement type) {
		return elements.getBinaryName(undeco(type));
	}

	/**
	 * @see javax.lang.model.util.Elements#getConstantExpression(java.lang.Object)
	 */
	public String getConstantExpression(Object value) {
		return elements.getConstantExpression(undeco(value));
	}

	/**
	 * @see javax.lang.model.util.Elements#getDocComment(javax.lang.model.element.Element)
	 */
	public String getDocComment(Element e) {
		return elements.getDocComment(undeco(e));
	}

	/**
	 * @see javax.lang.model.util.Elements#getElementValuesWithDefaults(javax.lang.model.element.AnnotationMirror)
	 */
	@Decorate
	public Map<? extends ExecutableElement, ? extends AnnotationValue> getElementValuesWithDefaults(
			AnnotationMirror a) {
		return elements.getElementValuesWithDefaults(undeco(a));
	}

	/**
	 * @see javax.lang.model.util.Elements#getPackageElement(java.lang.CharSequence)
	 */
	@Decorate
	public PackageElement getPackageElement(CharSequence name) {
		return elements.getPackageElement(name);
	}

	/**
	 * @see javax.lang.model.util.Elements#getTypeElement(java.lang.CharSequence)
	 */
	@Decorate
	public TypeElement getTypeElement(CharSequence name) {
		return elements.getTypeElement(name);
	}

	/**
	 * @see javax.lang.model.util.Elements#hides(javax.lang.model.element.Element, javax.lang.model.element.Element)
	 */
	public boolean hides(Element hider, Element hidden) {
		return elements.hides(undeco(hider), undeco(hidden));
	}

	/**
	 * @see javax.lang.model.util.Elements#isDeprecated(javax.lang.model.element.Element)
	 */
	public boolean isDeprecated(Element e) {
		return elements.isDeprecated(undeco(e));
	}

	/**
	 * @see javax.lang.model.util.Elements#overrides(javax.lang.model.element.ExecutableElement, javax.lang.model.element.ExecutableElement, javax.lang.model.element.TypeElement)
	 */
	public boolean overrides(ExecutableElement rider, ExecutableElement ridee,
			TypeElement type) {
		return elements.overrides(undeco(rider), undeco(ridee), undeco(type));
	}

	/**
	 * @see javax.lang.model.util.Elements#printElements(java.io.Writer, javax.lang.model.element.Element[])
	 */
	public void printElements(Writer w, Element... elements) {
		List<Element> es = new ArrayList<Element>();
		for (Element e : elements) es.add(undeco(e));
		this.elements.printElements(w, es.<Element>toArray(new Element[0]));
	}

	/**
	 * EXPERIMENTAL!!!
	 * Return parser tree for element
	 * @param element
	 * @return the parser tree
	 */
	public JCTree getParserTree(Element element) {
		if (elements instanceof JavacElements) {
			JavacElements javac = (JavacElements)elements;
			return javac.getTree(undeco(element));
		}
		return null;
	}
	
}
